{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c38c1fca",
   "metadata": {},
   "outputs": [],
   "source": [
    "from lightgbm import LGBMClassifier\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "58dd9b53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       A1BG      A1CF     A2LD1       A2M     A2ML1    A4GALT     A4GNT  \\\n",
      "0  3.575803  1.815535  3.407354  6.848847  1.949370  3.389573  1.935099   \n",
      "1  3.602704  1.903611  3.400847  6.504326  2.367093  3.035315  1.941405   \n",
      "2  4.089806  1.998825  3.733770  4.904531  2.208550  3.328281  1.923295   \n",
      "3  3.929732  1.930019  3.668371  3.957686  2.845188  2.855448  1.855836   \n",
      "4  3.645441  1.917237  3.594290  5.312689  2.115304  2.795185  1.731550   \n",
      "\n",
      "       AAA1      AAAS      AACS  ...     ZWINT      ZXDA      ZXDB      ZXDC  \\\n",
      "0  1.982839  4.669173  3.067800  ...  3.395375  6.346860  3.659673  2.529919   \n",
      "1  1.807694  4.240750  3.758705  ...  3.265353  5.657011  4.127937  2.186070   \n",
      "2  1.946061  4.597667  3.490880  ...  3.559725  5.708799  4.387543  1.922107   \n",
      "3  1.727771  4.024512  2.849850  ...  3.847016  6.144403  4.096720  2.144641   \n",
      "4  1.644886  4.463236  3.022631  ...  3.386223  5.636870  3.211165  1.893340   \n",
      "\n",
      "     ZYG11A    ZYG11B       ZYX     ZZEF1      ZZZ3  label  \n",
      "0  1.782195  3.769252  5.465261  3.541981  3.870610      1  \n",
      "1  2.042729  3.924281  4.821240  3.670514  4.405655      1  \n",
      "2  1.901744  4.126258  4.993717  3.913525  3.976327      1  \n",
      "3  1.893372  3.586055  5.175353  3.854349  4.095984      1  \n",
      "4  2.320021  4.140209  4.653637  3.509094  3.998673      1  \n",
      "\n",
      "[5 rows x 20229 columns]\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 424 entries, 0 to 423\n",
      "Columns: 20229 entries, A1BG to label\n",
      "dtypes: float64(20228), int64(1)\n",
      "memory usage: 65.4 MB\n"
     ]
    }
   ],
   "source": [
    "#데이터를 불러오고 형식 확인\n",
    "\n",
    "df = pd.read_csv('./cancer_dataset_ver1.csv')\n",
    "print(df.head())\n",
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "204b5341",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SMOTE 적용 전 학습용 피처/레이블 데이터 세트:  (339, 20228) (339,)\n",
      "SMOTE 적용 후 학습용 피처/레이블 데이터 세트:  (422, 20228) (422,)\n"
     ]
    }
   ],
   "source": [
    "#전처리(오버샘플링)\n",
    "\n",
    "from imblearn.over_sampling import SMOTE\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "X = df.iloc[:,:-1]\n",
    "y = df.iloc[:,-1]\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, stratify=y)\n",
    "smote = SMOTE()\n",
    "X_train_over, y_train_over = smote.fit_resample(X_train, y_train)\n",
    "print('SMOTE 적용 전 학습용 피처/레이블 데이터 세트: ', X_train.shape, y_train.shape)\n",
    "print('SMOTE 적용 후 학습용 피처/레이블 데이터 세트: ', X_train_over.shape, y_train_over.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7ea2889b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#성능지표 함수\n",
    "\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix, f1_score\n",
    "\n",
    "def get_clf_eval(y_test, pred):\n",
    "    confusion = confusion_matrix(y_test, pred)\n",
    "    accuracy = accuracy_score(y_test, pred)\n",
    "    precision = precision_score(y_test, pred)\n",
    "    recall = recall_score(y_test, pred)\n",
    "    f1 = f1_score(y_test,pred)\n",
    "\n",
    "    print('오차 행렬')\n",
    "    print(confusion)\n",
    "    print('정확도: {0:.4f}, 정밀도: {1:.4f}, 재현율: {2:.4f}, F1:{3:.4f}'.format(accuracy,precision, recall,f1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1f486575",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "오차 행렬\n",
      "[[24  8]\n",
      " [ 6 47]]\n",
      "정확도: 0.8353, 정밀도: 0.8545, 재현율: 0.8868, F1:0.8704\n"
     ]
    }
   ],
   "source": [
    "#랜덤포레스트 구현\n",
    "\n",
    "rf_clf = RandomForestClassifier(n_estimators = 1000, max_depth = 100)\n",
    "rf_clf.fit(X_train_over, y_train_over)\n",
    "pred = rf_clf.predict(X_test)\n",
    "get_clf_eval(y_test,pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "34a40cb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#랜덤포레스트를 이용하여 변수 중요도 추출\n",
    "ftr_importances_values = rf_clf.feature_importances_\n",
    "ftr_importances = pd.Series(ftr_importances_values, index = X_train_over.columns)\n",
    "ftr_top5000 = ftr_importances.sort_values(ascending=False)[:5000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "db1e63b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\jinsung\\appdata\\local\\programs\\python\\python39\\lib\\site-packages\\lightgbm\\sklearn.py:726: UserWarning: 'early_stopping_rounds' argument is deprecated and will be removed in a future release of LightGBM. Pass 'early_stopping()' callback via 'callbacks' argument instead.\n",
      "  _log_warning(\"'early_stopping_rounds' argument is deprecated and will be removed in a future release of LightGBM. \"\n",
      "c:\\users\\jinsung\\appdata\\local\\programs\\python\\python39\\lib\\site-packages\\lightgbm\\sklearn.py:736: UserWarning: 'verbose' argument is deprecated and will be removed in a future release of LightGBM. Pass 'log_evaluation()' callback via 'callbacks' argument instead.\n",
      "  _log_warning(\"'verbose' argument is deprecated and will be removed in a future release of LightGBM. \"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\ttraining's binary_logloss: 0.628102\tvalid_1's binary_logloss: 0.680244\n",
      "[2]\ttraining's binary_logloss: 0.575942\tvalid_1's binary_logloss: 0.637026\n",
      "[3]\ttraining's binary_logloss: 0.530128\tvalid_1's binary_logloss: 0.621354\n",
      "[4]\ttraining's binary_logloss: 0.490561\tvalid_1's binary_logloss: 0.612378\n",
      "[5]\ttraining's binary_logloss: 0.451589\tvalid_1's binary_logloss: 0.594998\n",
      "[6]\ttraining's binary_logloss: 0.417421\tvalid_1's binary_logloss: 0.577629\n",
      "[7]\ttraining's binary_logloss: 0.3881\tvalid_1's binary_logloss: 0.55921\n",
      "[8]\ttraining's binary_logloss: 0.360496\tvalid_1's binary_logloss: 0.544647\n",
      "[9]\ttraining's binary_logloss: 0.333716\tvalid_1's binary_logloss: 0.536104\n",
      "[10]\ttraining's binary_logloss: 0.308153\tvalid_1's binary_logloss: 0.523808\n",
      "[11]\ttraining's binary_logloss: 0.287626\tvalid_1's binary_logloss: 0.51293\n",
      "[12]\ttraining's binary_logloss: 0.267442\tvalid_1's binary_logloss: 0.517546\n",
      "[13]\ttraining's binary_logloss: 0.248798\tvalid_1's binary_logloss: 0.50678\n",
      "[14]\ttraining's binary_logloss: 0.23145\tvalid_1's binary_logloss: 0.497374\n",
      "[15]\ttraining's binary_logloss: 0.217595\tvalid_1's binary_logloss: 0.497822\n",
      "[16]\ttraining's binary_logloss: 0.202664\tvalid_1's binary_logloss: 0.490688\n",
      "[17]\ttraining's binary_logloss: 0.188817\tvalid_1's binary_logloss: 0.47996\n",
      "[18]\ttraining's binary_logloss: 0.176231\tvalid_1's binary_logloss: 0.473694\n",
      "[19]\ttraining's binary_logloss: 0.165087\tvalid_1's binary_logloss: 0.467323\n",
      "[20]\ttraining's binary_logloss: 0.154364\tvalid_1's binary_logloss: 0.462644\n",
      "[21]\ttraining's binary_logloss: 0.145088\tvalid_1's binary_logloss: 0.459594\n",
      "[22]\ttraining's binary_logloss: 0.136164\tvalid_1's binary_logloss: 0.453495\n",
      "[23]\ttraining's binary_logloss: 0.128069\tvalid_1's binary_logloss: 0.451233\n",
      "[24]\ttraining's binary_logloss: 0.120006\tvalid_1's binary_logloss: 0.448004\n",
      "[25]\ttraining's binary_logloss: 0.111959\tvalid_1's binary_logloss: 0.440544\n",
      "[26]\ttraining's binary_logloss: 0.104735\tvalid_1's binary_logloss: 0.43629\n",
      "[27]\ttraining's binary_logloss: 0.0986008\tvalid_1's binary_logloss: 0.433501\n",
      "[28]\ttraining's binary_logloss: 0.0929975\tvalid_1's binary_logloss: 0.433127\n",
      "[29]\ttraining's binary_logloss: 0.087041\tvalid_1's binary_logloss: 0.42459\n",
      "[30]\ttraining's binary_logloss: 0.0819589\tvalid_1's binary_logloss: 0.419221\n",
      "[31]\ttraining's binary_logloss: 0.0771544\tvalid_1's binary_logloss: 0.415497\n",
      "[32]\ttraining's binary_logloss: 0.0715306\tvalid_1's binary_logloss: 0.41194\n",
      "[33]\ttraining's binary_logloss: 0.0667571\tvalid_1's binary_logloss: 0.410728\n",
      "[34]\ttraining's binary_logloss: 0.0628205\tvalid_1's binary_logloss: 0.408052\n",
      "[35]\ttraining's binary_logloss: 0.0593199\tvalid_1's binary_logloss: 0.400852\n",
      "[36]\ttraining's binary_logloss: 0.055152\tvalid_1's binary_logloss: 0.394816\n",
      "[37]\ttraining's binary_logloss: 0.0519248\tvalid_1's binary_logloss: 0.396687\n",
      "[38]\ttraining's binary_logloss: 0.0490609\tvalid_1's binary_logloss: 0.39998\n",
      "[39]\ttraining's binary_logloss: 0.046063\tvalid_1's binary_logloss: 0.39974\n",
      "[40]\ttraining's binary_logloss: 0.0429548\tvalid_1's binary_logloss: 0.401213\n",
      "[41]\ttraining's binary_logloss: 0.0405648\tvalid_1's binary_logloss: 0.401108\n",
      "[42]\ttraining's binary_logloss: 0.0380511\tvalid_1's binary_logloss: 0.406178\n",
      "[43]\ttraining's binary_logloss: 0.035795\tvalid_1's binary_logloss: 0.405104\n",
      "[44]\ttraining's binary_logloss: 0.0334603\tvalid_1's binary_logloss: 0.404089\n",
      "[45]\ttraining's binary_logloss: 0.0316307\tvalid_1's binary_logloss: 0.400383\n",
      "[46]\ttraining's binary_logloss: 0.0295901\tvalid_1's binary_logloss: 0.400886\n",
      "[47]\ttraining's binary_logloss: 0.0277643\tvalid_1's binary_logloss: 0.391876\n",
      "[48]\ttraining's binary_logloss: 0.026065\tvalid_1's binary_logloss: 0.389481\n",
      "[49]\ttraining's binary_logloss: 0.0245115\tvalid_1's binary_logloss: 0.386091\n",
      "[50]\ttraining's binary_logloss: 0.0230188\tvalid_1's binary_logloss: 0.384661\n",
      "[51]\ttraining's binary_logloss: 0.0215472\tvalid_1's binary_logloss: 0.382074\n",
      "[52]\ttraining's binary_logloss: 0.0201755\tvalid_1's binary_logloss: 0.380787\n",
      "[53]\ttraining's binary_logloss: 0.0189348\tvalid_1's binary_logloss: 0.381228\n",
      "[54]\ttraining's binary_logloss: 0.017802\tvalid_1's binary_logloss: 0.380972\n",
      "[55]\ttraining's binary_logloss: 0.0167952\tvalid_1's binary_logloss: 0.379558\n",
      "[56]\ttraining's binary_logloss: 0.0158567\tvalid_1's binary_logloss: 0.378745\n",
      "[57]\ttraining's binary_logloss: 0.0150124\tvalid_1's binary_logloss: 0.374871\n",
      "[58]\ttraining's binary_logloss: 0.0140805\tvalid_1's binary_logloss: 0.372622\n",
      "[59]\ttraining's binary_logloss: 0.0133703\tvalid_1's binary_logloss: 0.369001\n",
      "[60]\ttraining's binary_logloss: 0.012554\tvalid_1's binary_logloss: 0.371142\n",
      "[61]\ttraining's binary_logloss: 0.0118392\tvalid_1's binary_logloss: 0.367111\n",
      "[62]\ttraining's binary_logloss: 0.011036\tvalid_1's binary_logloss: 0.366246\n",
      "[63]\ttraining's binary_logloss: 0.0103666\tvalid_1's binary_logloss: 0.364253\n",
      "[64]\ttraining's binary_logloss: 0.00970031\tvalid_1's binary_logloss: 0.365736\n",
      "[65]\ttraining's binary_logloss: 0.00911057\tvalid_1's binary_logloss: 0.361327\n",
      "[66]\ttraining's binary_logloss: 0.00851425\tvalid_1's binary_logloss: 0.366156\n",
      "[67]\ttraining's binary_logloss: 0.00795904\tvalid_1's binary_logloss: 0.364786\n",
      "[68]\ttraining's binary_logloss: 0.00743192\tvalid_1's binary_logloss: 0.365365\n",
      "[69]\ttraining's binary_logloss: 0.00695941\tvalid_1's binary_logloss: 0.368223\n",
      "[70]\ttraining's binary_logloss: 0.00656851\tvalid_1's binary_logloss: 0.369088\n",
      "[71]\ttraining's binary_logloss: 0.00618431\tvalid_1's binary_logloss: 0.366248\n",
      "[72]\ttraining's binary_logloss: 0.00582577\tvalid_1's binary_logloss: 0.363452\n",
      "[73]\ttraining's binary_logloss: 0.00544755\tvalid_1's binary_logloss: 0.363746\n",
      "[74]\ttraining's binary_logloss: 0.00512813\tvalid_1's binary_logloss: 0.361494\n",
      "[75]\ttraining's binary_logloss: 0.00483915\tvalid_1's binary_logloss: 0.362895\n",
      "[76]\ttraining's binary_logloss: 0.00451838\tvalid_1's binary_logloss: 0.364932\n",
      "[77]\ttraining's binary_logloss: 0.00424715\tvalid_1's binary_logloss: 0.367005\n",
      "[78]\ttraining's binary_logloss: 0.0039808\tvalid_1's binary_logloss: 0.367225\n",
      "[79]\ttraining's binary_logloss: 0.00375088\tvalid_1's binary_logloss: 0.370246\n",
      "[80]\ttraining's binary_logloss: 0.00354876\tvalid_1's binary_logloss: 0.376203\n",
      "[81]\ttraining's binary_logloss: 0.00333979\tvalid_1's binary_logloss: 0.37977\n",
      "[82]\ttraining's binary_logloss: 0.00316547\tvalid_1's binary_logloss: 0.373674\n",
      "[83]\ttraining's binary_logloss: 0.00296458\tvalid_1's binary_logloss: 0.378126\n",
      "[84]\ttraining's binary_logloss: 0.0027936\tvalid_1's binary_logloss: 0.379616\n",
      "[85]\ttraining's binary_logloss: 0.00261929\tvalid_1's binary_logloss: 0.382249\n",
      "[86]\ttraining's binary_logloss: 0.00247768\tvalid_1's binary_logloss: 0.388334\n",
      "[87]\ttraining's binary_logloss: 0.00232435\tvalid_1's binary_logloss: 0.38988\n",
      "[88]\ttraining's binary_logloss: 0.00218715\tvalid_1's binary_logloss: 0.395263\n",
      "[89]\ttraining's binary_logloss: 0.00205424\tvalid_1's binary_logloss: 0.396867\n",
      "[90]\ttraining's binary_logloss: 0.00194711\tvalid_1's binary_logloss: 0.39925\n",
      "[91]\ttraining's binary_logloss: 0.00184025\tvalid_1's binary_logloss: 0.402257\n",
      "[92]\ttraining's binary_logloss: 0.0017287\tvalid_1's binary_logloss: 0.400704\n",
      "[93]\ttraining's binary_logloss: 0.00162385\tvalid_1's binary_logloss: 0.405739\n",
      "[94]\ttraining's binary_logloss: 0.00153119\tvalid_1's binary_logloss: 0.405793\n",
      "[95]\ttraining's binary_logloss: 0.0014411\tvalid_1's binary_logloss: 0.405176\n",
      "[96]\ttraining's binary_logloss: 0.00135515\tvalid_1's binary_logloss: 0.40693\n",
      "[97]\ttraining's binary_logloss: 0.00126945\tvalid_1's binary_logloss: 0.406979\n",
      "[98]\ttraining's binary_logloss: 0.00119773\tvalid_1's binary_logloss: 0.407928\n",
      "[99]\ttraining's binary_logloss: 0.00112871\tvalid_1's binary_logloss: 0.410977\n",
      "[100]\ttraining's binary_logloss: 0.00105759\tvalid_1's binary_logloss: 0.411581\n",
      "[101]\ttraining's binary_logloss: 0.000992027\tvalid_1's binary_logloss: 0.411674\n",
      "[102]\ttraining's binary_logloss: 0.000931992\tvalid_1's binary_logloss: 0.412716\n",
      "[103]\ttraining's binary_logloss: 0.000879344\tvalid_1's binary_logloss: 0.406914\n",
      "[104]\ttraining's binary_logloss: 0.000826302\tvalid_1's binary_logloss: 0.405066\n",
      "[105]\ttraining's binary_logloss: 0.000765789\tvalid_1's binary_logloss: 0.406612\n",
      "[106]\ttraining's binary_logloss: 0.000721012\tvalid_1's binary_logloss: 0.4121\n",
      "[107]\ttraining's binary_logloss: 0.000682352\tvalid_1's binary_logloss: 0.412752\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[108]\ttraining's binary_logloss: 0.000641751\tvalid_1's binary_logloss: 0.41118\n",
      "[109]\ttraining's binary_logloss: 0.00060456\tvalid_1's binary_logloss: 0.411875\n",
      "[110]\ttraining's binary_logloss: 0.000567988\tvalid_1's binary_logloss: 0.41003\n",
      "[111]\ttraining's binary_logloss: 0.000536591\tvalid_1's binary_logloss: 0.414702\n",
      "[112]\ttraining's binary_logloss: 0.000499434\tvalid_1's binary_logloss: 0.412798\n",
      "[113]\ttraining's binary_logloss: 0.000468815\tvalid_1's binary_logloss: 0.41271\n",
      "[114]\ttraining's binary_logloss: 0.000440706\tvalid_1's binary_logloss: 0.40936\n",
      "[115]\ttraining's binary_logloss: 0.000416882\tvalid_1's binary_logloss: 0.408749\n",
      "[116]\ttraining's binary_logloss: 0.000391238\tvalid_1's binary_logloss: 0.410169\n",
      "[117]\ttraining's binary_logloss: 0.0003691\tvalid_1's binary_logloss: 0.407345\n",
      "[118]\ttraining's binary_logloss: 0.000347508\tvalid_1's binary_logloss: 0.414392\n",
      "[119]\ttraining's binary_logloss: 0.000324296\tvalid_1's binary_logloss: 0.412624\n",
      "[120]\ttraining's binary_logloss: 0.000305731\tvalid_1's binary_logloss: 0.411103\n",
      "[121]\ttraining's binary_logloss: 0.000287474\tvalid_1's binary_logloss: 0.412973\n",
      "[122]\ttraining's binary_logloss: 0.00027039\tvalid_1's binary_logloss: 0.415394\n",
      "[123]\ttraining's binary_logloss: 0.000255398\tvalid_1's binary_logloss: 0.421491\n",
      "[124]\ttraining's binary_logloss: 0.000241167\tvalid_1's binary_logloss: 0.426042\n",
      "[125]\ttraining's binary_logloss: 0.000227707\tvalid_1's binary_logloss: 0.430455\n",
      "[126]\ttraining's binary_logloss: 0.00021511\tvalid_1's binary_logloss: 0.427051\n",
      "[127]\ttraining's binary_logloss: 0.000200488\tvalid_1's binary_logloss: 0.430498\n",
      "[128]\ttraining's binary_logloss: 0.000187358\tvalid_1's binary_logloss: 0.432255\n",
      "[129]\ttraining's binary_logloss: 0.000177137\tvalid_1's binary_logloss: 0.435638\n",
      "[130]\ttraining's binary_logloss: 0.00016568\tvalid_1's binary_logloss: 0.436338\n",
      "[131]\ttraining's binary_logloss: 0.00015553\tvalid_1's binary_logloss: 0.43849\n",
      "[132]\ttraining's binary_logloss: 0.000146374\tvalid_1's binary_logloss: 0.440499\n",
      "[133]\ttraining's binary_logloss: 0.000138124\tvalid_1's binary_logloss: 0.441287\n",
      "[134]\ttraining's binary_logloss: 0.000130531\tvalid_1's binary_logloss: 0.438456\n",
      "[135]\ttraining's binary_logloss: 0.000122434\tvalid_1's binary_logloss: 0.437436\n",
      "[136]\ttraining's binary_logloss: 0.000115691\tvalid_1's binary_logloss: 0.442582\n",
      "[137]\ttraining's binary_logloss: 0.000108397\tvalid_1's binary_logloss: 0.448434\n",
      "[138]\ttraining's binary_logloss: 0.000102259\tvalid_1's binary_logloss: 0.448967\n",
      "[139]\ttraining's binary_logloss: 9.68685e-05\tvalid_1's binary_logloss: 0.453015\n",
      "[140]\ttraining's binary_logloss: 9.13226e-05\tvalid_1's binary_logloss: 0.450745\n",
      "[141]\ttraining's binary_logloss: 8.62309e-05\tvalid_1's binary_logloss: 0.449375\n",
      "[142]\ttraining's binary_logloss: 8.09565e-05\tvalid_1's binary_logloss: 0.453039\n",
      "[143]\ttraining's binary_logloss: 7.61839e-05\tvalid_1's binary_logloss: 0.453439\n",
      "[144]\ttraining's binary_logloss: 7.22782e-05\tvalid_1's binary_logloss: 0.453464\n",
      "[145]\ttraining's binary_logloss: 6.82672e-05\tvalid_1's binary_logloss: 0.451279\n",
      "[146]\ttraining's binary_logloss: 6.38503e-05\tvalid_1's binary_logloss: 0.452991\n",
      "[147]\ttraining's binary_logloss: 5.97358e-05\tvalid_1's binary_logloss: 0.450325\n",
      "[148]\ttraining's binary_logloss: 5.63267e-05\tvalid_1's binary_logloss: 0.45414\n",
      "[149]\ttraining's binary_logloss: 5.30887e-05\tvalid_1's binary_logloss: 0.459054\n",
      "[150]\ttraining's binary_logloss: 5.0098e-05\tvalid_1's binary_logloss: 0.463949\n",
      "[151]\ttraining's binary_logloss: 4.73132e-05\tvalid_1's binary_logloss: 0.469482\n",
      "[152]\ttraining's binary_logloss: 4.47449e-05\tvalid_1's binary_logloss: 0.469567\n",
      "[153]\ttraining's binary_logloss: 4.20867e-05\tvalid_1's binary_logloss: 0.468622\n",
      "[154]\ttraining's binary_logloss: 4.0022e-05\tvalid_1's binary_logloss: 0.46937\n",
      "[155]\ttraining's binary_logloss: 3.80253e-05\tvalid_1's binary_logloss: 0.471607\n",
      "[156]\ttraining's binary_logloss: 3.61963e-05\tvalid_1's binary_logloss: 0.482661\n",
      "[157]\ttraining's binary_logloss: 3.44917e-05\tvalid_1's binary_logloss: 0.483772\n",
      "[158]\ttraining's binary_logloss: 3.28796e-05\tvalid_1's binary_logloss: 0.488571\n",
      "[159]\ttraining's binary_logloss: 3.12575e-05\tvalid_1's binary_logloss: 0.495736\n",
      "[160]\ttraining's binary_logloss: 2.98877e-05\tvalid_1's binary_logloss: 0.499199\n",
      "[161]\ttraining's binary_logloss: 2.86751e-05\tvalid_1's binary_logloss: 0.501343\n",
      "[162]\ttraining's binary_logloss: 2.73371e-05\tvalid_1's binary_logloss: 0.507231\n",
      "[163]\ttraining's binary_logloss: 2.60629e-05\tvalid_1's binary_logloss: 0.508408\n",
      "[164]\ttraining's binary_logloss: 2.49666e-05\tvalid_1's binary_logloss: 0.506655\n",
      "[165]\ttraining's binary_logloss: 2.37621e-05\tvalid_1's binary_logloss: 0.509857\n",
      "오차 행렬\n",
      "[[22 10]\n",
      " [ 5 48]]\n",
      "정확도: 0.8235, 정밀도: 0.8276, 재현율: 0.9057, F1:0.8649\n"
     ]
    }
   ],
   "source": [
    "#top5000을 이용해 LGBM 모델 구현\n",
    "\n",
    "df = pd.read_csv('./cancer_dataset_ver1.csv')\n",
    "df_new = df[ftr_top5000.index].copy()\n",
    "df_new['label'] = df['label']\n",
    "df_new.head()\n",
    "X = df_new.iloc[:, :-1]\n",
    "y = df_new.iloc[:,-1]\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, stratify=y)\n",
    "smote = SMOTE()\n",
    "X_train_over, y_train_over = smote.fit_resample(X_train, y_train)\n",
    "X_tr, X_val, y_tr, y_val = train_test_split(X_train_over, y_train_over, test_size = 0.2)\n",
    "lgbm = LGBMClassifier(n_estimators = 500, learning_rate = 0.1, max_depth = 100)\n",
    "evals = [(X_tr, y_tr), (X_val, y_val)]\n",
    "lgbm.fit(X_tr, y_tr, early_stopping_rounds = 100, eval_metric = \"logloss\", eval_set = evals, verbose = True)\n",
    "preds = lgbm.predict(X_test)\n",
    "get_clf_eval(y_test, preds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d1b1e7c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "1 교차 검증 정확도: 0.8353, 학습 데이터 크기: 339, 검증 데이터 크기: 85\n",
      "\n",
      "2 교차 검증 정확도: 0.8706, 학습 데이터 크기: 339, 검증 데이터 크기: 85\n",
      "\n",
      "3 교차 검증 정확도: 0.8353, 학습 데이터 크기: 339, 검증 데이터 크기: 85\n",
      "\n",
      "4 교차 검증 정확도: 0.8235, 학습 데이터 크기: 339, 검증 데이터 크기: 85\n",
      "\n",
      "5 교차 검증 정확도: 0.7976, 학습 데이터 크기: 340, 검증 데이터 크기: 84\n",
      "\n",
      "## 교차 검증별 정확도:  [0.8353 0.8706 0.8353 0.8235 0.7976]\n",
      "\n",
      "## 평균 검증 정확도:  0.8325\n"
     ]
    }
   ],
   "source": [
    "#SKFold 이용하여 측정\n",
    "\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "\n",
    "df = pd.read_csv('./cancer_dataset_ver1.csv')\n",
    "df_new = df[ftr_top5000.index].copy()\n",
    "df_new['label'] = df['label']\n",
    "df_new.head()\n",
    "X = df_new.iloc[:, :-1]\n",
    "y = df_new.iloc[:,-1]\n",
    "\n",
    "skfold = StratifiedKFold(n_splits = 5)\n",
    "\n",
    "scores = []\n",
    "n_iter=0\n",
    "for train_index, test_index in skfold.split(X, y):\n",
    "    X_train, y_train = X.iloc[train_index], y.iloc[train_index]\n",
    "    X_test, y_test = X.iloc[test_index],y.iloc[test_index]\n",
    "    \n",
    "    lgbm_clf = LGBMClassifier(n_estimators = 300, max_depth = 100, learning_rate = 0.1)\n",
    "    lgbm_clf.fit(X_train, y_train)\n",
    "    pred = lgbm_clf.predict(X_test)\n",
    "    n_iter +=1\n",
    "    accuracy = np.round(accuracy_score(y_test, pred), 4)\n",
    "    train_size = X_train.shape[0]\n",
    "    test_size = X_test.shape[0]\n",
    "    print('\\n{0} 교차 검증 정확도: {1}, 학습 데이터 크기: {2}, 검증 데이터 크기: {3}'.format(n_iter, accuracy, train_size, test_size))\n",
    "    scores.append(accuracy)\n",
    "print('\\n## 교차 검증별 정확도: ', np.round(scores, 4))\n",
    "print('\\n## 평균 검증 정확도: ', np.round(np.mean(scores),4))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc9043d9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "257bc933",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb50fc4f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
